﻿<?
require("../../common/init.php");
require("author.php");
require("upfile.php");
require(ROOT."common/output_csv.php");
 
// $data=array();
// array_push($data,array('姓名','年龄'));
// array_push($data,array('davee','18','kenneth','28'));
 
 
// //$fp = fopen('file.csv', 'w');

// // send response headers to the browser
// header( 'Content-Type: text/csv' );
// header( 'Content-Disposition: attachment;filename='.date('YmdHis').rand(111,999).'.csv');
// $fp = fopen('php://output', 'w');

// fprintf($fp, chr(0xEF).chr(0xBB).chr(0xBF)); 
// foreach ($data as $fields){
//     fputcsv($fp, $fields);
// }

// fclose($fp);

// exit;

$shipment_method=new DB(Tshipment_method,$conn);
$sales_order=new DB(Tsales_order,$conn);
$sales_order_detail=new DB(Tsales_order_detail,$conn);
$gee=$sales_order; // gee, 意为赶车时候喊的"驾", 此处用来表示次页面的主角对象
//$cate=$product_category; // cate = category, 分类

$title=ROOT_ORDER_REPORT;
$url='report_sales';
// 默认7天内订单
$tem = mktime(0,0,0,date("m"),date("d")-7,date("Y"));
$weekago = date("Y-m-d", $tem);
// 开始日期
$checkin=isset($_GET["checkin"])?clean($_GET["checkin"]):$weekago; // search
// 结束日期
$checkout=isset($_GET["checkout"])?clean($_GET["checkout"]):date('Y-m-d');

// 整理不合格日期, tidy invalid date
$tem_checkin=strtotime($checkin);
$tem_checkout=strtotime($checkout);
$datediff=datediff('d',$tem_checkin,$tem_checkout);
if($datediff<1) $tem_checkin=dateadd('d',-7,$tem_checkout);
$checkin=date('Y-m-d',$tem_checkin);

// varible 变量
$do=isset($_GET["do"])?clean($_GET["do"]):'view'; // 操作: 查看/编辑/删除 
$action=isset($_POST["action"])?clean($_POST["action"]):''; // 数据库操作: CURD 
$page=1; if(isset($_GET["page"]))if(is_numeric($_GET["page"]))$page=$_GET["page"]; // current page 当前页码
$id=0; if (isset($_GET["id"])) $id = clean($_GET["id"]);
$export=0; if (isset($_GET["export"])) $export = clean($_GET["export"]);

// province 省
$arr_province=array();
$tem=$province->get_rows(1024);
foreach ($tem as $val){
	$arr_province[$val['id']]=$val;
}

// city 市
$arr_city=array();
$tem=$city->get_rows(10240);
foreach ($tem as $val){
	$arr_city[$val['id']]=$val;
}

// county 区
$arr_county=array();
$tem=$county->get_rows(10240);
foreach ($tem as $val){
	$arr_county[$val['id']]=$val;
}

// payment method 支付方式
$arr_payment=array();
$tem=$payment_method->get_rows(32);
foreach ($tem as $val){
	$arr_payment[$val['id']]=$val;
}

// shipment method 寄送方式
$arr_shipment=array();
$tem=$shipment_method->get_rows(32);
foreach ($tem as $val){
	$arr_shipment[$val['id']]=$val;
}
// pre($arr_payment);
// pre($arr_shipment);
$where=array();
$where[]=array('name'=>'created_time','val'=>$checkin,'opn'=>'>');
$where[]=array('name'=>'created_time','val'=>$checkout,'opn'=>'<');

if($export==0){ // 显示在页面 show in page
	$order='';
	$pages=$gee->count_pages($where);
	$info=$gee->get_page($page,$where);
	// $info=$gee->get_rows_advanced(1024,$where,'id','desc',1);
	// while($row=$info->fetch(PDO::FETCH_BOTH) ){
	// 	print_r($row);
	// }
	// exit;
}
else { // 导出到CSV, export to CSV
	$head=array('#','订单号','E-mail','产品','货币','金额','收件地址','电话','收件人','客户备注','收款方式','支付时间','支付者','物流方式','运费','	我司发货时间','物流单号','操作备注','订单状态	','下单时间');
	$info=$gee->get_rows_advanced(102400,$where,'id','desc',1);
	output_csv($head,$info);
	// while($row=$info->fetch(PDO::FETCH_BOTH) ){
	// 	print_r($row);
	// }
	exit;
}

function update($info)
{
	global $gee, $url, $h, $id;
	if($id==0) 
	{
		echo "Invali argument!!!";
		exit;
	}
	echo $h->form($url.'.php', 0, '');
  	show_box($info,true, false);
	echo $h->hidden('action', 'update');
	echo $h->former();
}
function delete($id)
{
	global $gee,$url;
	if($dave->delete($uid)) wheel($url.'.php');
}	
function show_info()
{
	global $gee,$info,$id;
	if($id==0) show_boxes($info);
	else show_box($info, false, false);
}
function sql_update()
{
	global $gee,$url, $filter;
	$id=iso(@$_POST["id"]);
	$arr=array(
		'shipped_time'=>iso(@$_POST["shipped_time"]),
		'shipment_tracking_no'=>iso(@$_POST["shipment_tracking_no"]),
		'order_status'=>iso(@$_POST["order_status"]),
		'operator_remark'=>iso(@$_POST["operator_remark"]),
		);
	if($gee->update($arr, 'id', $id)) yell(ROOT_HANDLE_SUCCESS,$url.'.php'.(($filter!="")?"?filter=".$filter:""));
	else echo mysql_error();
}

// view 加载视图
require ROOTVIEW.$url.'.php';

// end sales_order.php